#include "httpconn.hpp"

// 记录当前用户fd,默认值-1
int httpconn::_epoll_fd = -1;
// 记录当前连接服务器用户总数
int httpconn::_conn_num = 0;

int SET_NOBLOCK_FD(const int &fd)
{
    int flag = fcntl(fd, F_GETFL);
    return fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}

extern int ADD_FD(const int &ep_fd, const int &app_fd, bool one_shot)
{
    struct epoll_event event;
    event.data.fd = app_fd;
    event.events = EPOLLIN | EPOLLHUP;
    if (one_shot)
        event.events |= EPOLLONESHOT;
    return epoll_ctl(ep_fd, EPOLL_CTL_ADD, app_fd, &event);
}

extern int DELETE_FD(const int &ep_fd, const int &del_fd)
{
    return epoll_ctl(ep_fd, EPOLL_CTL_DEL, del_fd, nullptr);
}

extern int MOD_FD(const int &ep_fd, const int &fd, int ev)
{
    struct epoll_event event;
    event.data.fd = fd;
    event.events = ev | EPOLLRDHUP | EPOLLONESHOT | EPOLLET;
    return epoll_ctl(ep_fd, EPOLL_CTL_MOD, fd, &event);
}

// 初始化函数
void httpconn::init(const int &fd, const struct sockaddr_in &conn_addr)
{
    _sock_fd = fd;
    _conn_addr = conn_addr;
    setsockopt(_sock_fd, SOL_SOCKET, SO_REUSEADDR, (void*)true, sizeof(bool));
    SET_NOBLOCK_FD(_sock_fd);
    // 连接用户服务器总数+1
    _conn_num++;
}

// 读取函数
void httpconn::read()
{
}

// 写入函数
void httpconn::write()
{
}